package com.example.demo.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.example.demo.dao.InformationDao;
import com.example.demo.entity.EtInformation;
import com.example.demo.entity.EtInformationType;
import com.example.demo.entity.EtUser;
import com.example.demo.service.InformationService;
import com.example.demo.util.ResultPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

@Service
public class InformationServiceImpl implements InformationService {

	@Resource
	private InformationDao informationDao;
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public int insertInformation(EtInformation info,String createdAt,HttpServletRequest request) throws ParseException {
		int i=0;
		//Long time=System.currentTimeMillis();
		
		EtUser user=((EtUser) request.getSession().getAttribute("etUser"));
		Long time=sdf.parse(createdAt).getTime();
		info.setUpdatedBy(user.getUserId());
		info.setUpdatedAt(time);
		if(StringUtil.isEmpty(info.getId())) {
			info.setId(UUID.randomUUID().toString());
			info.setIsDeleted(0);
			info.setCreatedBy(user.getUserId());
			info.setCreatedAt(time);
			if("3".equals(info.getType())) {
				EtInformation report=informationDao.getReport(info.getReportNum(), info.getReportCode());
				if(report!=null) {
					return 2;
				}
			}
			i=informationDao.insertInformation(info);
		}else {
			
			i=informationDao.updateInformation(info);
		}
		
		return i;
	}

	@Override
	public ResultPage<EtInformation> getInfoList(EtInformation info) {
		ResultPage<EtInformation> page = new ResultPage<EtInformation>();
		int bTablePageNum = (info.getOffset() / info.getLimit());
		PageHelper.startPage(bTablePageNum + 1, info.getLimit());
		List<EtInformation> list = new ArrayList<>();
		list = informationDao.getInfoList(info);
		PageInfo<EtInformation> table = new PageInfo<EtInformation>(list);
		page.setTotal(table.getTotal());
		page.setRows(table.getList());
		return page;
	}

	@Override
	public EtInformation getInfo(String id) {
		EtInformation info=informationDao.getInfo(id);
		if(info==null) {
			return new EtInformation();
		}
		info=informationDao.getDetailInfo(id, info.getType());
		
		return info;
	}

	@Override
	public List<EtInformation> getTitleList(String type) {
		List<EtInformation> list=informationDao.getTitleList(type);
		if(list==null) {
			list=new ArrayList<>();
		}
		return list;
	}

	@Override
	public List<EtInformationType> getTypeList() {
		List<EtInformationType> list=informationDao.getTypeList();
		if(list==null) {
			list=new ArrayList<>();
		}
		return list;
	}


	@Override
	public int delInfo(String id) {
		int i=informationDao.delInformation(id);
		return i;
	}

	@Override
	public EtInformation getReport(String reportNum, String reportCode) {
		EtInformation report=informationDao.getReport(reportNum, reportCode);
		
		return report;
	}

}
